//
//  FBNetworkTool.m
//  Fireball
//
//  Created by 俞渊华 on 2017/12/2.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "FBNetworkTool.h"
#import <MJExtension.h>
#import "FBNetWorkUrlTool.h"   //处理URL


@implementation FBNetworkTool


+ (__kindof NSURLSessionTask *)get:(NSString *)URL
                        parameters:(id)parameters
                           showHUD:(BOOL)showHUD
                           success:(DBHttpRequestSuccess)success
                           failure:(XMHttpRequestFailed)failure{
    return [self get:URL parameters:parameters showHUD:showHUD responseCache:^(XMHttpResponseModel *responseModel) {
        
    } success:success failure:failure];
}

+ (__kindof NSURLSessionTask *)get:(NSString *)URL
                        parameters:(id)parameters
                           showHUD:(BOOL)showHUD
                     responseCache:(DBHttpRequestCache)cache
                           success:(DBHttpRequestSuccess)success
                           failure:(XMHttpRequestFailed)failure{
    URL = [FBNetWorkUrlTool urlString:URL parameters:parameters];
    if (showHUD) {//显示加载动画
        [MBProgressHUD showHUDAddedTo:nil animated:YES];
    }
    //这里的parameters不需要参数
    return [XMNetworkManager GET:URL parameters:nil responseCache:^(id responseCache) {
        if (responseCache) {
            XMHttpResponseModel * model = [XMHttpResponseModel mj_objectWithKeyValues:responseCache];
            cache(model);
        }
    } success:^(id responseObject) {
        
        XMHttpResponseModel * model = [XMHttpResponseModel mj_objectWithKeyValues:responseObject];
        
        [MBProgressHUD hideAllHUDsForView:nil animated:YES];
        if (model.code == 200) {
            if (success) { success(model); }
        }else{
            success(model);
            if (showHUD) {
                [MBProgressHUD showError:model.msg];
            }
        }
        
    } failure:^(NSError *error) {
        if (showHUD) {
            [MBProgressHUD hideAllHUDsForView:nil animated:YES];
            [MBProgressHUD showError:error.localizedDescription toView:nil];
        }
        if (failure) {
            failure(error);
        }
        
    }];
}

+ (__kindof NSURLSessionTask *)post:(NSString *)URL
                         parameters:(id)parameters
                            showHUD:(BOOL)showHUD
                            success:(DBHttpRequestSuccess)success
                            failure:(XMHttpRequestFailed)failure{
    
    
    if (showHUD) {//显示加载动画
        [MBProgressHUD showHUDAddedTo:nil animated:YES];
    }
    //    打开打印日志
    //    [XMNetworkManager openLog];
    return [XMNetworkManager POST:URL parameters:parameters success:^(id responseObject) {
        
        XMHttpResponseModel * model = [XMHttpResponseModel mj_objectWithKeyValues:responseObject];
        
//        [MBProgressHUD hideAllHUDsForView:nil animated:YES];
//        if (model.isSuccess == 1) {
//            if (success) { success(model); }
//        }else{
//            success(model);
//            if (showHUD) {
////                [MBProgressHUD showError:responseObject[@"errorMsg"]];
//            }
//        }
    } failure:^(NSError *error) {
        if (showHUD) {
//            [MBProgressHUD hideAllHUDsForView:nil animated:YES];
//            [MBProgressHUD showError:error.localizedDescription toView:nil];
        }
        if (failure) {
            failure(error);
        }
    }];
    
}


+ (__kindof NSURLSessionTask *)post:(NSString *)URL
                         parameters:(id)parameters
                            showHUD:(BOOL)showHUD
                      responseCache:(DBHttpRequestCache)cache
                            success:(DBHttpRequestSuccess)success
                            failure:(XMHttpRequestFailed)failure{
//    URL = [DBNetWorkUrlTool handleUrl:URL];
//    URL = [URL stringByAppendingString:[DBNetWorkUrlTool handleParams:parameters]];
    if (showHUD) {
        [MBProgressHUD showHUDAddedTo:nil animated:YES];
    }
    
    return [XMNetworkManager POST:URL parameters:parameters responseCache:^(id responseCache) {
        XMHttpResponseModel * model = [XMHttpResponseModel mj_objectWithKeyValues:responseCache];
        cache(model);
    } success:^(id responseObject) {
        XMHttpResponseModel * model = [XMHttpResponseModel mj_objectWithKeyValues:responseObject];
        
//        [MBProgressHUD hideAllHUDsForView:nil animated:YES];
//        if (model.isSuccess == 1) {
//            if (success) {
//                success(model);
//            }else{
//                success(model);
//                [MBProgressHUD showError:responseObject[@"errorMsg"]];
//            }
//        }else{
//            success(model);
//            if (showHUD) {
//                [MBProgressHUD showError:responseObject[@"errorMsg"]];
//            }
//        }
        
    } failure:^(NSError *error) {
//        if (showHUD) {
//            [MBProgressHUD hideAllHUDsForView:nil animated:YES];
//            [MBProgressHUD showError:error.localizedDescription toView:nil];
//        }
        if (failure) {
            failure(error);
        }
        
    }];
}



@end
